﻿using System;
using System.Collections.Generic;
using System.Dynamic;

namespace TradeDataAPI.Core.Common
{
    public class TimeHelper
    {



        /// <summary>
        /// 获取传入日期的1月1号号或者12月31号
        /// </summary>
        /// <param name="dateStr"></param>
        /// <param name="firstDay"></param>
        /// <returns></returns>
        public static string ChangeToFirstOrLastDayOfMonth(string dateStr, bool firstDay = true)
        {
            DateTime date = DateTime.Parse(dateStr);

            DateTime newDate;
            if (firstDay)
            {
                // 获取当月的第一天
                newDate = new DateTime(date.Year, date.Month, 1);
            }
            else
            {
                // 获取当月的最后一天
                newDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
            }

            // 转换回字符串格式
            return newDate.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 计算周期
        /// </summary>
        /// <returns></returns>
        public static List<string> GetTimeList()
        {

            DateTime Start = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            List<string> list = new List<string>();
            DateTime today = DateTime.Now;
            int day = today.Day;
            int month = today.Month;
            int year = today.Year;

            // 计算当前周期的开始和结束日期
            DateTime cycleStart;
            DateTime cycleEnd;

            if (day >= 21)
            {
                cycleStart = new DateTime(year, month, 21);
                cycleEnd = new DateTime(year, month + 1, 20);
            }
            else
            {
                cycleStart = new DateTime(year, month - 1, 21);
                cycleEnd = new DateTime(year, month, 20);
            }

            // 判断当前日期是否在周期内
            bool isInCycle = today >= cycleStart && today <= cycleEnd;

            // 列出周期内的所有日期
            if (isInCycle)
            {
                //Console.WriteLine($"当前日期在周期内。距离周期截止日还有：{(cycleEnd - today).Days}天");
                //Console.WriteLine("周期内的所有日期如下：");

                for (DateTime date = cycleStart; date <= cycleEnd; date = date.AddDays(1))
                {
                    if (date >= Start)
                    {
                        list.Add(date.ToShortDateString());
                    }
                }
            }
            else
            {
                Console.WriteLine("当前日期不在周期内。");
            }


            return list;

        }

        /// <summary>
        /// 获取某天中某个时刻的
        /// </summary>
        /// <param name="dateTime">短日期</param>
        /// <param name="Hours">某个时间</param>
        /// <returns></returns>
        public static DateTime GetTime(DateTime dateTime, int Hours, int Minutes = 0)
        {

            // 假设你有一个特定的日期，例如 2024 年 10 月 14 日
            DateTime specificDate = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);
            // 创建该日期下午 16:00:00 的时间
            var targetTime = new DateTime(specificDate.Year, specificDate.Month, specificDate.Day, Hours, Minutes, 0);


            return targetTime;

        }



        /// <summary>
        /// 计算两个日期间隔的所有日期
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static List<string> GetDatesBetween(DateTime start, DateTime end)
        {
            List<string> dates = new List<string>();

            for (DateTime date = start; date <= end; date = date.AddDays(1))
            {
                dates.Add(date.ToShortDateString());
            }

            return dates;
        }




        /// <summary>
        /// 获取当前月的对象信息
        /// </summary>
        /// <returns></returns>
        public static IDictionary<string, object> GenerateMonthlyData()
        {
            var MonthName = DateTime.Now.Month;
            var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 21); // 假设当月21号是2024年10月21日
            var endDate = startDate.AddMonths(1).AddDays(-1); // 下个月20号

            dynamic data = new ExpandoObject();
            var dataDict = (IDictionary<string, object>)data;

            // 添加 WorkNumber 字段
            dataDict["工号"] = ""; // 这里可以替换为实际的 WorkNumber 值
            for (var date = startDate; date <= endDate; date = date.AddDays(1))
            {
                string fieldName;
                if (date.Day <= 20)
                {
                    if (MonthName >= 12)
                    {
                        fieldName = $"{1}/{date.Day}";
                    }
                    else
                    {
                        fieldName = $"{MonthName + 1}/{date.Day}";
                    }
                }
                else
                {
                    fieldName = $"{MonthName}/{date.Day}";
                }

                dataDict[fieldName] = $"Value for {fieldName}"; // 这里可以替换为实际数据
            }


            return dataDict;

        }

    }
}
